#! /bin/sh

download_source()
{
    # Initialize variables
    spec_sources=$(spectool -S "$1" | awk -F ': ' '{print $2}')
    proxy="https://cache-proxy.test.osinfra.cn/download"
    target_dir="$2"

    # Iterate over the sources
    for source in $spec_sources; do
        case "$source" in
            http://*)
                ;;
            https://*)
                ;;
            *)
                continue
                ;;
        esac

        # Construct the proxy URL
        obs_source="$proxy/$source"

        # Download from the cache proxy
        base_name=$(basename "$source")
        wget "$obs_source" -P "$target_dir"

        # Check if the file was successfully downloaded
        if [ ! -f "$target_dir/$base_name" ]; then
            echo "Failed to download from cache-proxy. Start downloading from the official website."
            wget "$source" -P "$target_dir"
        fi
    done
}

# vim:set ts=4 sw=4 et:
